package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {
    @Resource
    private DeptMapper  deptMapper;

    @Resource
    private EmpExprService empExprService;

    @Resource
    private DeptService deptService;
    @Override
    public PageResult<Emp> queryPage(EmpQueryParam param) {

        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));
        Page<Emp> pageInfo = lambdaQuery().like(StringUtils.hasLength(param.getName()), Emp::getName, param.getName())
                .eq(null != param.getGender(), Emp::getGender, param.getGender())
                .ge(null != param.getBegin(), Emp::getEntryDate, param.getBegin())
                .le(null != param.getEnd(), Emp::getEntryDate, param.getEnd())
                .page(page);

        //获取所有员工的部门id
        List<Integer> deptIds = pageInfo.getRecords().stream().map(emp -> emp.getDeptId()).collect(Collectors.toList());

        List<Dept> deptList = deptMapper.selectBatchIds(deptIds);

        //遍历员工列表,为每一个员工设置部门名称
        pageInfo.getRecords().forEach(emp -> {
            deptList.stream().filter(dept -> dept.getId().equals(emp.getDeptId()))
                    .findFirst()
                    .ifPresent(dept -> emp.setDeptName(dept.getName()));
        });

        return new PageResult(page.getTotal(),pageInfo.getRecords());
    }

    /**
     * 新增员工
     * @param emp
     */
    @Override
    public void saveEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(emp.getCreateTime());

        save(emp);

        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(expr ->expr.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
    }

    /**
     * 查询回显
     * @param id
     * @return
     */
    @Override
    public Emp getByEmpId(Integer id) {
        Emp emp = getById(id);
        emp.setDeptName(deptMapper.selectById(emp.getDeptId()).getName());
        emp.setExprList(empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,id)));
        return emp;
    }

    /**
     * 更新员工信息
     * @param emp
     */
    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);

        //删除员工工作经历
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,emp.getId()));

        //批量保存员工的工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if (null != exprList && !exprList.isEmpty()) {
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
        }
        //插入
        empExprService.saveBatch(exprList);
    }

    /*@Resource
    private EmpMapper empMapper;

    public void update() {
        List<Long> ids = List.of(1L, 2L, 3L);
        int amount = 100;

        LambdaQueryWrapper<Emp> wrapper =  new LambdaQueryWrapper<Emp>()
                .in(Emp::getId,ids);
        empMapper.updateBalanceByIds(wrapper,amount);

    }*/


}
